package com.yr.cus;

import lombok.Data;
import org.springframework.web.server.ServerWebExchange;

import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

public class PredicateFactory extends AbstractRoutePredicateFactory<AgeRoutePredicateFactory.Config>{
    public AgeRoutePredicateFactory() {
        super(Config.class);
    }
    @Override
    public List<String> shortcutFieldOrder() {
        return Arrays.asList("min","max");
    }

    @Override
    public Predicate<ServerWebExchange> apply(AgeRoutePredicateFactory.Config config) {
        return exchange -> {
            String ageStr = exchange.getRequest().getQueryParams().getFirst("age");
            Integer age = Integer.parseInt(ageStr);
            return age >= config.getMin() && age <= config.getMax();
        };
    }
    @Data
    public static class Config {
        private Integer min;
        private Integer max;
    }
}
